﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace StarControl
{
    class ActionScreen : GameScreen
    {
        KeyboardState keyboardState;
        Texture2D image;
        Rectangle imageRectangle;
        Vector2[] posicaoNavesSelecionadas;
        Texture2D[] naves;
        bool carregou=false;
        public PlayerScreen playerScreen;

        MileniumFalconShip mShip;
        PegasusShip pShip;
        StarfighterShip sfShip;
        ValkyrieShip vShip;
        Limpeza limpa;
        Colisao colisao;

        public Obstaculo[] obstaculos;

        public bool acabouJogo;
        
        public ActionScreen(Game game, SpriteBatch spriteBatch, Texture2D image, PlayerScreen _playerScreen)
            : base(game, spriteBatch)
        {
            this.image = image;
            imageRectangle = new Rectangle(
            0,
            0,
            Game.Window.ClientBounds.Width,
            Game.Window.ClientBounds.Height);

            posicaoNavesSelecionadas = new Vector2[2];
            posicaoNavesSelecionadas[0] = new Vector2(700,100);
            posicaoNavesSelecionadas[1] = new Vector2(700, 400);

            playerScreen = _playerScreen;

            naves = new Texture2D[2];

            acabouJogo = false;

            Initialize();
        }

        public override void Initialize()
        {

            base.Initialize();
            colisao = new Colisao(this.game);
            //IsMouseVisible = true;
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

        }
        protected override void UnloadContent()
        {
        }
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            keyboardState = Keyboard.GetState();

            if (playerScreen.navesSelecionadas != null && !carregou)
            {
                limpa = new Limpeza(this.game);
                carregou = true;
                int i = 0;
                foreach (TipoNave.Tipo tp in playerScreen.navesSelecionadas)
                {
                    switch (tp)
                    {
                        case TipoNave.Tipo.MileniunShip:
                            naves[i] = Game.Content.Load<Texture2D>("Textures/naves/Millenium");
                            /* Instancia a MilleniumFalcom com uma escudo de invisibilidade */
                            mShip = new MileniumFalconShip(this.game, i+1);
                            mShip.setBatch(spriteBatch);
                            Components.Add(mShip);
                            break;
                        case TipoNave.Tipo.Pegasus:
                            naves[i] = Game.Content.Load<Texture2D>("Textures/naves/Pegasus");
                            pShip = new PegasusShip(this.game, i+1);
                            pShip.setBatch(spriteBatch);
                            Components.Add(pShip);
                            break;
                        case TipoNave.Tipo.Starfigther:
                            naves[i] = Game.Content.Load<Texture2D>("Textures/naves/Starfight");
                            sfShip = new StarfighterShip(this.game, i+1);
                            sfShip.setBatch(spriteBatch);
                            Components.Add(sfShip);
                            break;
                        case TipoNave.Tipo.Valkyrie:
                            naves[i] = Game.Content.Load<Texture2D>("Textures/naves/Valkyrie");
                            vShip = new ValkyrieShip(this.game, i+1);
                            vShip.setBatch(spriteBatch);
                            Components.Add(vShip);
                            break;
                    }
                    i++;
                }

                #region "Asteroides"
                //Adiciona os obstaculos (qtde que quiser)
                limpa.gerenciarObstaculos(this.spriteBatch);
                #endregion "Asteroides"
            }

            limpa.Update(gameTime);

            // Gerencia o numero de obstaculos na tela
            limpa.gerenciarObstaculos(this.spriteBatch);

            colisao.ocorreuColisaoTiroMeteoro();

            foreach (TipoNave.Tipo tp in playerScreen.navesSelecionadas)
            {
                switch(tp)
                {
                    case TipoNave.Tipo.MileniunShip:
                        if (mShip != null)
                            if (!mShip.isAlive)
                                acabouJogo = true;
                        break;
                    case TipoNave.Tipo.Pegasus:
                        if (pShip != null)
                            if (!pShip.isAlive)
                                acabouJogo = true;
                        break;
                    case TipoNave.Tipo.Starfigther:
                        if (sfShip != null)
                            if (!sfShip.isAlive)
                                acabouJogo = true;
                        break;
                    case TipoNave.Tipo.Valkyrie:
                        if (vShip != null)
                            if (!vShip.isAlive)
                                acabouJogo = true;
                        break;
                }
            }

            if (keyboardState.IsKeyDown(Keys.Escape))
                game.Exit();
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(image, imageRectangle, Color.White);
            spriteBatch.End();
            /*for (int i = 0; i < 2; i++)
            {
                spriteBatch.Draw(naves[i], posicaoNavesSelecionadas[i], Color.White);
            }*/

            /* Tenta desenhar a explosão */
            base.Draw(gameTime);

            List<Explosao> explosoesList = colisao.recuperaComponentesExplosao();
            foreach (Explosao exp in explosoesList) // Loop through List with foreach
            {
                Console.WriteLine("Explodindooo");
                // kkkkkk
                if (exp.explodindo)
                {
                    exp.update();
                    exp.Draw(gameTime, spriteBatch);
                }
                else
                {
                    Console.WriteLine("acabou a explosao");
                    exp.Dispose();
                }
            }
        }

        public void Limpar()
        {
            naves = new Texture2D[2];
            acabouJogo = false;

            keyboardState = new KeyboardState();
            carregou = false;
            limpa = new Limpeza(this.game);
            colisao = new Colisao(this.game);
            acabouJogo = false;
            Components.Remove(mShip);
            Components.Remove(pShip);
            Components.Remove(sfShip);
            Components.Remove(vShip);
        }

    }
}
